//
//  TLMallListTableViewCell.m
//  WXReader
//
//  Created by huaya on 2025/1/13.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLMallListTableViewCell.h"
@interface TLMallListTableViewCell()
@property (weak, nonatomic) IBOutlet UILabel *bookNameLab;
@property (weak, nonatomic) IBOutlet UILabel *bookDescLab;
@property (weak, nonatomic) IBOutlet UILabel *bookMoreDescLab;
@property (weak, nonatomic) IBOutlet UIImageView *bookImgView;
@property (weak, nonatomic) IBOutlet UILabel *favLab;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (nonatomic, strong) NSMutableArray *mArr;
@end

@implementation TLMallListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.mArr = [NSMutableArray new];
    self.bookImgView.layer.masksToBounds = YES;
    self.bookImgView.layer.cornerRadius = 8;
    self.bgView.layer.cornerRadius = 10;
    _bookNameLab.textColor = kMainColor;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)setModel:(TLMallChannelModel *)model{
    if (model) {
        _model = model;
        
        _bookNameLab.text = model.worksName;
        
        _bookDescLab.text = model.recommended;
        
        _bookMoreDescLab.text = model.desc;
        
        _favLab.text = model.collectNum.stringValue;
        
        [self.bookImgView sd_setImageWithURL:[NSURL URLWithString:model.coverPic]];
        
        NSArray *tags = [model.tagsName componentsSeparatedByString:@","];
        
        if (tags.count>0 && ![model.tagsName isEqualToString:@""]) {
            
            [self createTagWithTags:tags];
            
        }
        
    }
}
- (void)createTagWithTags:(NSArray *)tags{
    
    CGFloat x = 8 + CGRectGetMaxX(self.bookImgView.frame);
    CGFloat y = CGRectGetMaxY(self.bookImgView.frame)-20;
    
    
    for (UILabel *v in self.mArr) {
        if (v.tag >= 100) {
            [v removeFromSuperview];
        }
    }
    
    for (int i=0; i<tags.count; i++) {
        
        if (i>0) {
            break;
        }
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 75, 20)];
        lab.textColor = [UIColor colorWithHexString:@"#999999"];
        lab.font =  [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
        lab.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        lab.text = [NSString stringWithFormat:@"  %@  ",tags[i]];
        [lab sizeToFit];
        CGRect frame = lab.frame;
        frame.size.height = 20;
        lab.frame = frame;
        lab.tag = 100+i;
        
        lab.layer.masksToBounds = YES;
        lab.layer.cornerRadius = 10;
        
        x = x + frame.size.width + 8;
        
        [self.bgView addSubview:lab];
        [self.mArr addObject:lab];
    }
    
}
@end
